package com.yiqi.liebang.feature.home.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.luck.picture.lib.rxbus2.RxBus;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.common.util.RxKeyboardTool;
import com.yiqi.liebang.feature.home.contract.QuestionContract;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity implements TopicFragment.CallBackValue ,QuestionFragment.CallBackValue{


    @BindView(R.id.tl_4)
    SlidingTabLayout mTabLayout;

    @BindView(R.id.vp)
    ViewPager mVp;
    @BindView(R.id.edt_search_text)
    EditText mEdtSearchText;

    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private final String[] mTitles = {
            "话题", "问答", "好友/人脉"
    };
    private CharSequence content;

    @Override
    protected void onStart() {
        super.onStart();
        mEdtSearchText.requestFocus();

    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_search;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mFragments.add(new TopicFragment());
        mFragments.add(new QuestionFragment());
        mFragments.add(new FriendsFragment());
        mTabLayout.setViewPager(mVp, mTitles, this, mFragments);

        mEdtSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){//搜索按键action
                    RxKeyboardTool.hideSoftInput(SearchActivity.this);
                    content = mEdtSearchText.getText().toString();
                    if (TextUtils.isEmpty(content)){
                        showMsg("请输入搜索内容！");
                        return true;
                    }
                    RxBus.getDefault().post(new EventEntity(1,content.toString()));
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void initData() {
        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
    }


    @Override
    public void SendMessageValue(boolean strValue) {
        RxKeyboardTool.showSoftInput(SearchActivity.this,mEdtSearchText);
    }
}
